#
# "@(#) $Id: LPixmapPushButton.py,v 1.1.1.1 2006-07-27 03:16:08 gioshe Exp $"
#
# This work is released under the GNU GPL, version 2 or later.
#
from Utils.kdeemul import *
#
# a version of KPushButton that simply displays the largest version of the
# first element in its QIconSet
#
class PixmapPushButton(KPushButton):
	def __init__(self,iconset,label,container,iconsetDown=None,bgColor=None):
		KPushButton.__init__(self,label,container)
		self.iconset=iconset
		self.iconsetDown=iconsetDown
		self.changeIconSet()
		self.bgColor=bgColor
##		self.setIconSet(self.iconset)
##		pixmap = self.pixmap()
##		self.setFixedSize(pixmap.size())
		#if iconsetDown!=None:
		#	QObject.connect(self,SIGNAL("pressed()"),self.changeIconSet)
		#	QObject.connect(self,SIGNAL("released()"),self.changeIconSet)

	def doErase(self):
		p = QPainter()
		p.begin(self)
		if self.backgroundMode() == Qt.X11ParentRelative:
			self.erase(0,0,self.width(),self.height())
		elif self.parentWidget() and self.parentWidget().erasePixmap():
			p.drawTiledPixmap(0,0,self.width(),self.height(),self.parentWidget().erasePixmap(),self.x(),self.y())
		else:
			if self.bgColor==None:
				p.fillRect(0,0,self.width(),self.height(),self.colorGroup().brush(QColorGroup.Background))
			else:
				p.fillRect(0,0,self.width(),self.height(),QBrush(self.bgColor))
		p.end()
	
	def pixmap(self):
		return self.iconSet().pixmap(QIconSet.Large,QIconSet.Normal)
	
	def changeIconSet(self):
		if self.isDown():
			#print "Button down",self.iconsetDown
			if not self.iconsetDown!=None:
				self.setIconSet(self.iconsetDown)
				pixmap = self.pixmap()
				self.setFixedSize(pixmap.size())
		else:
			#print "Button UP",self.iconset
			self.setIconSet(self.iconset)
			pixmap = self.pixmap()
			self.setFixedSize(pixmap.size())

	def paintEvent(self,e):
		self.doErase()
		pixmap = self.pixmap()
		srcSize = pixmap.size()
		dstSize = self.size()
		posX = (dstSize.width()-srcSize.width())/2
		posY = (dstSize.height()-srcSize.height())/2
		bitBlt(self,QPoint(posX,posY),pixmap,pixmap.rect(),Qt.CopyROP)

class SmallPixmapPushButton(PixmapPushButton):
	def __init__(self,iconset,label,container,iconDown=None,bgColor=None):
		PixmapPushButton.__init__(self,iconset,label,container,iconDown,bgColor)
	
	def pixmap(self):
		if self.iconSet():
			return self.iconSet().pixmap(QIconSet.Small,QIconSet.Normal)
		else:
			return QPixmap()
